import request from '@/utils/request';
import { encrypt, decrypt } from '@/utils/jsencrypt';
import {
  encryptBase64,
  encryptWithAes,
  generateAesKey,
  decryptWithAes,
  decryptBase64,
} from '@/utils/crypto';

// 账号登录
export function login(data) {
  // 生成一个 AES 密钥
  const aesKey = generateAesKey();
  return request({
    url: '/auth/login',
    method: 'post',
    data: encryptWithAes(JSON.stringify(data), aesKey),
    isJson: true,
    headers: {
      'encrypt-key': encrypt(encryptBase64(aesKey)),
    },
  });
}

// 验证码图片
export function getCode(data) {
  return request({
    url: '/auth/code',
    method: 'get',
    data,
    isJson: true,
  });
}

// 短信验证码
export function getSmsCode(phonenumber) {
  return request({
    url: `/resource/sms/code?t=${Date.now()}`,
    method: 'get',
    data: { phonenumber },
    isJson: true,
  });
}

// 菜单数据
export function getRoleMenu(data) {
  return request({
    url: '/park/roleMenuXcx/listSelect',
    method: 'get',
    data,
    isJson: true,
  });
}
// 最新版本
export function latestVersion() {
  return request({
    url: '/park/appHistory/latestVersion',
    method: 'get',
    isShowToast: false,
  });
}

// 用户数据
export function getUserList(data) {
  return request({
    url: '/system/user/list',
    method: 'get',
    data,
    isJson: true,
  });
}

// 用户所有数据
export function getUserAllList(data) {
  return request({
    url: '/system/user/listAll',
    method: 'get',
    data,
    isJson: true,
  });
}

// 用户数据，非token登录
export function getUserListNoToken(data) {
  return request({
    url: '/workflow/visitorInvite/userList',
    method: 'post',
    data,
    isJson: true,
  });
}

// OSS图片
export function getOssList(data) {
  return request({
    url: '/resource/oss/listByIds/' + data,
    method: 'get',
    isJson: true,
  });
}

// 空间tree数据
export function getSpaceList(data) {
  return request({
    url: '/park/space/list',
    method: 'get',
    isJson: true,
  });
}

// 扫码qrCode返回空间id
export function getSpaceIdByQrCode(data) {
  return request({
    url: '/park/space/getSpaceIdByQrCode',
    method: 'get',
    data,
    isJson: true,
  });
}

// 人员数据
export function getPersonList(data) {
  return request({
    url: '/park/personnel/getList',
    method: 'get',
    data,
    isJson: true,
  });
}

// 部门数据
export function getDeptList(data) {
  return request({
    url: '/system/dept/list',
    method: 'get',
    data,
    isJson: true,
  });
}

// 用户部门数据
export function getDeptTreeList(data) {
  return request({
    url: '/system/user/deptTreeNew',
    method: 'get',
    data,
    isJson: true,
  });
}

// 人员部门数据
export function getPDeptList(data) {
  return request({
    url: '/park/department/getList',
    method: 'get',
    data,
    isJson: true,
  });
}

// 获取系统字典数据
export function getDictList(data) {
  return request({
    url: '/system/dict/data/type/' + data,
    method: 'get',
    isJson: true,
  });
}

// 使用说明
export function getInstructions(menuXcxNo) {
  return request({
    url: '/park/menuXcx/instructions',
    method: 'get',
    data: { menuXcxNo },
    isJson: true,
  });
}

// 修改密码
export function editPsw(data) {
  return request({
    url: '/auth/updatePwd',
    method: 'put',
    data,
    isJson: true,
  });
}
